package tracker;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class DelegatingVehicleTracker implements VehicleTracker {
    private final ConcurrentMap<String, SimplePoint> locations;

    public DelegatingVehicleTracker(Map<String, SimplePoint> points) {
        locations = new ConcurrentHashMap<String, SimplePoint>(points);
    }

    public Map<String, Point> getLocations() {
        return Collections.unmodifiableMap(
                new HashMap<String, Point>(locations));
    }

    public Point getLocation(String id) {
        return locations.get(id);
    }

    public void setLocation(String id, int x, int y) {
        if (locations.replace(id, new SimplePoint(x, y)) == null)
            throw new IllegalArgumentException(
                "invalid vehicle name: " + id);
    }
}
